package org.celstec.mobilegame.client;


import org.celstec.mobilegame.client.MapsCallback;
import org.celstec.mobilegame.client.SubmitGameService;
import org.celstec.mobilegame.client.SubmitGameServiceAsync;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Position;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.map.GoogleMap;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Image;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.Viewport;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.layout.*;
import com.gwtext.client.widgets.map.*;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.*;
import com.gwtext.client.widgets.Button;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Locatory implements EntryPoint {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private static final SubmitGameServiceAsync submitService = GWT
			.create(SubmitGameService.class);
	private static Locatory thisInstance;

	public static GoogleMap mapWidget = null;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		thisInstance = this;
		Panel panel = new Panel();
		panel.setBorder(false);
		panel.setPaddings(15);
		panel.setLayout(new FitLayout());
		panel.setBorder(false);
		panel.setPaddings(15);
		Panel borderPanel = new Panel();
		borderPanel.setLayout(new BorderLayout());

		// Panel westPanel = new Panel();
		// // westPanel.setHtml("<p>west panel</p>");
		// westPanel.setTitle("Configure Game");
		// westPanel.setBodyStyle("background-color:#EEEEEE");
		// westPanel.setCollapsible(true);
		//
		// westPanel.setWidth(400);
		//
		// BorderLayoutData westData = new
		// BorderLayoutData(RegionPosition.WEST);
		// // westData.setSplit(true);
		// westData.setMinSize(175);
		// westData.setMaxSize(400);
		// westData.setSplit(true);
		// westData.setMargins(new Margins(0, 5, 0, 0));
		// westPanel.add(getFormPanel());
		//
		// borderPanel.add(westPanel, westData);

		Panel centerPanel = new Panel();
		centerPanel.setLayout(new FitLayout());

		Toolbar toolbar = new Toolbar();
		toolbar.setTitle("test");
		// setToolbarButtons( toolbar);
		ToolbarButton button = new ToolbarButton("Upload game",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						showUploadGamePanel();
					}
				});

		ToolbarButton PlayButton = new ToolbarButton("Play game",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						submitService
								.getAvailableGames((AsyncCallback) thisInstance);
					}
				});
		toolbar.addButton(button);
		toolbar.addButton(PlayButton);

		centerPanel.setTopToolbar(toolbar);

		borderPanel.add(centerPanel,
				new BorderLayoutData(RegionPosition.CENTER));

		
		if (MapFactory.isGoogleAvailable()) {
			mapWidget = (GoogleMap) MapFactory.getMap();

		}
		mapWidget.setHeight(400);
		mapWidget.setWidth(800);
//		mapWidget.addControl(new MapT); 
		mapWidget.addLargeControls();
		mapWidget.addMapTypeControls();

		double lat = 50.877842;
		double lon = 5.959294;
		LatLonPoint latLonPoint = new LatLonPoint(lat, lon);
		mapWidget.setCenterAndZoom(latLonPoint, 16);
		// mapPanel.addMarker(new Marker(latLonPoint));

		centerPanel.add(mapWidget);
		panel.add(borderPanel);
		Viewport viewport = new Viewport(panel);
		viewport.doLayout();

	}

	final TextField GameName = new TextField("Name Game", "Name");
	private static String gameString ="";

	public void showUploadGamePanel() {
		final Window window = new Window();
		window.setClosable(true);
		window.setButtonAlign(Position.CENTER);
		window.setPlain(true);
		window.setLayout(new FitLayout());
		window.setTitle("Upload game");

		final TextArea xmlArea = new TextArea("Game XML", "xml");
		Button loginButton = new Button("Send", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				gameString = GameName.getText();
				submitService.submitGame(xmlArea.getText(), gameString,
						(AsyncCallback) thisInstance);
				window.destroy();
			}
		});

		FormPanel formPanel = new FormPanel();
		formPanel.setBaseCls("x-plain");
		formPanel.setLabelWidth(80);

		xmlArea.setWidth(500);
		xmlArea.setHeight(400);

		formPanel.add(GameName, new AnchorLayoutData("100%"));
		formPanel.add(xmlArea);
		window.add(formPanel);
		window.addButton(loginButton);
		window.show();
	}

	public void showPlayGamePanel(String[] allGames) {
		final Window window = new Window();
		window.setClosable(true);
		window.setButtonAlign(Position.CENTER);
		window.setPlain(true);
		window.setLayout(new FitLayout());
		window.setTitle("Create Play");

		FormPanel formPanel = new FormPanel();
		formPanel.setBaseCls("x-plain");
		formPanel.setLabelWidth(80);

		final TextField playName = new TextField("Name Play", "Name");
		String data[][] = new String[allGames.length][3];
		for (int i = 0; i < allGames.length; i++) {
			data[i] = new String[] { "test", allGames[i], "dummy" };
		}
		final Store store = new SimpleStore(new String[] { "abbr", "game",
				"nick" }, data);
		store.load();

		final ComboBox cb = new ComboBox();
		cb.setForceSelection(true);
		cb.setMinChars(1);
		cb.setFieldLabel("Games");
		cb.setStore(store);
		cb.setDisplayField("game");
		cb.setMode(ComboBox.LOCAL);
		cb.setTriggerAction(ComboBox.ALL);
		cb.setEmptyText("select a game");
		cb.setLoadingText("Searching...");
		cb.setTypeAhead(true);
		cb.setSelectOnFocus(true);
		cb.setWidth(200);

		Button loginButton = new Button("Send", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				playNameString = playName.getText();
				submitService.createPlay(playName.getText(), cb.getText(),
						(AsyncCallback) thisInstance);

				window.destroy();
			}
		});

		formPanel.add(playName, new AnchorLayoutData("100%"));
		formPanel.add(cb, new AnchorLayoutData("100%"));

		window.add(formPanel);
		window.addButton(loginButton);
		window.show();
	}

	public void showBarcode() {
		final Window window = new Window();
		window.setClosable(true);
		window.setButtonAlign(Position.CENTER);
		window.setPlain(true);
		window.setLayout(new FitLayout());
		window.setTitle("Barcode");
		Image im = new Image(
				"http://qrcode.kaywa.com/img.php?s=8&d=http%3A%2F%2Fcelstec1.appspot.com%2Flocatory?p="
						+ playNameString);
		// im.setHeight(400);
		window.add(im);
		window.setHeight(400);
		window.setWidth(400);

		// window.add(formPanel);
		// window.addButton(loginButton);
		window.show();
	}

	private Panel getFormPanel() {
		Panel outer = new Panel();
		outer.setLayout(new ColumnLayout());

		outer.setButtonAlign(Position.CENTER);

		final TextField PlayName = new TextField("Play", "Play");
		final TextArea xmlArea = new TextArea("Game", "Game");
		Button loginButton = new Button("Send", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				submitService.submitGame(xmlArea.getText(), PlayName.getText(),
						(AsyncCallback) thisInstance);
			}
		});

		FormPanel formPanel = new FormPanel();
		formPanel.setBaseCls("x-plain");
		formPanel.setLabelWidth(55);

		xmlArea.setHeight(100);
		formPanel.add(PlayName, new AnchorLayoutData("100%"));
		formPanel.add(xmlArea, new AnchorLayoutData("100%"));

		// outer.add(intro);
		outer.add(formPanel);

		outer.addButton(loginButton);
		Image im = new Image(
				"http://qrcode.kaywa.com/img.php?s=8&d=http%3A%2F%2Fwww.google.com"
						+ playNameString);
		// im.setHeight(400);
		// im.setHeight(300);
		// im.setWi
		outer.add(im);
		return outer;
	}

	public void onFailure(Throwable caught) {
		// TODO Auto-generated method stub

	}

	private static String playNameString = "";

	public void onSuccess(Object result) {
		if (result.getClass().getName().contains("[Ljava.lang.String;")) {
			String[] retString = (String[]) result;

			showPlayGamePanel(retString);

		} else if (result.getClass().getName().contains("java.lang.Integer")) {
			Integer resultInteger = (Integer) result;
			switch (resultInteger.intValue()) {
			case 1:
				showBarcode();
				break;
			case 2:
				submitService.getLongLatPairs(gameString, 
						new MapsCallback());
				break;

			default:
				break;
			}

		}
		// TODO Auto-generated method stub

	}
}
